package com.shop.shoporder.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.qiniu.util.Json;
import com.shop.shoporder.common.HttpRequest;
import com.shop.shoporder.common.MessageConstants;
import com.shop.shoporder.common.Response;
import com.shop.shoporder.common.UserRequest;
import com.shop.shoporder.entity.RedisUserNo;
import com.shop.shoporder.service.OrderSettlementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Controller
public class Test {

    @Autowired
    OrderSettlementService orderSettlementService;

    @Autowired
    RedisTemplate redisTemplate;

    @ResponseBody
    @RequestMapping(value = "/Settlement")
    public Response Settlement(HttpServletRequest request) {
        if(orderSettlementService.selectSettlement())
        {
            return new Response(MessageConstants.MSG_SUCCESS, null, "结算完成", request);
        }
        return new Response(MessageConstants.MSG_ACCESS_DENY, null, "接口参数错误", request);
    }

    //缓存用户数据
    @ResponseBody
    @RequestMapping(value = "/getRedisUser")
    public String getRedisUser(HttpServletRequest request) {
        RedisUserNo redisUserNo = JSON.parseObject(redisTemplate.opsForValue().get("ID92192").toString(), new TypeReference<RedisUserNo>() {
        });
        return "sss:"+redisUserNo.getNo();
    }

    //缓存用户数据
    @ResponseBody
    @RequestMapping(value = "/getIp")
    public String getIp(HttpServletRequest request) {
        return "sss:"+ UserRequest.getIp(request);
    }

}
